/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ids.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.sql.*;

/**
 *
 * @author isuru
 */
public class Config {

    private static ArrayList<String> uriList;
    
    private static int maxCIHBCount;

    static
    {
        uriList = new ArrayList<String>();
//        uriList.add("192.168.1.2");
//        uriList.add("192.168.1.6");
//        uriList.add("192.168.1.4");
        
        maxCIHBCount = 5;
    }

    public static ArrayList<String> getAllUris()
    {
        //return if url list is already full
        if (uriList!=null && uriList.size()>0 ){
            
            return uriList;
        }
        
        uriList = new ArrayList<String>();
        //get the URLs from the DB
            // JDBC driver name and database URL
   final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   final String DB_URL = "jdbc:mysql://localhost/intelidis";

   //  Database credentials
   final String USER = "root";
   final String PASS = "";
   
   Connection conn = null;
   Statement stmt = null;
   
   try{
     
      Class.forName("com.mysql.jdbc.Driver");

      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      stmt = conn.createStatement();

      String sql = "SELECT * FROM intelidis.serverstab";
      ResultSet rs = stmt.executeQuery(sql);
      //STEP 5: Extract data from result set
      while(rs.next()){
         uriList.add(rs.getString("ServerIP"));
      }
      rs.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            conn.close();
      }catch(SQLException se){
      }// do nothing
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
        return uriList;
    }

    public static void addUri(String uri)
    {
        uriList.add(uri);
    }

    public static void removeUri(String uri)
    {
        uriList.remove(uri);
    }

    public static int getUriCount()
    {
        return uriList.size();
    }

    /**
     * @return the maxCIHBCount
     */
    static public int getMaxCIHBCount() {
        return maxCIHBCount;
    }

    /**
     * @param maxCIHBCount the maxCIHBCount to set
     */
    static public void setMaxCIHBCount(int maxCIHBCount) {
        maxCIHBCount = maxCIHBCount;
    }
}
